$('#courses').live("pageinit", function(event) {
	createCoursesPage();
});

/* Creates the coursesPage 	*/
function createCoursesPage() {
	$('#coursesList').empty();
	$.ajax( { 
		url: "/api/user/vak/",
		dataType:"json",
		success:	function(data) { 
			fillCoursesList(data.result);	// fill the courses list with courses
		},
		error: function() {
			console.log("Error: Can not load json object");
		}
	});

	
	/* Fill the page Courses with the results.		*
	 * Needs a JSON object of the courses		 	*/
	function fillCoursesList(courses) {
		var coursesList = $('#coursesList');												// returns the <ul> with id coursesList
 		for( var i=0;i<courses.length;i++) {
			var li = $('<li><a href="#" class="view">'+courses[i].name+'</a></li>');
			li.find('a[class="view"]').jqmData('course',courses[i]).click(viewCourse);
			coursesList.append(li);													// add the <li> element to the courses list
		}
		coursesList.listview('refresh');													// refresh the courses list
	}
	
	/* Creates the page of the selected course and loads the page*/
	function viewCourse() {
		course = $(this).jqmData('course');		// get the course
		
		localStorage.removeItem("course");
		localStorage["course"] = JSON.stringify(course);
		
		var moreQuestions = {"bool":"false"};
		localStorage["moreQuestions"] = JSON.stringify(moreQuestions);

//		var moreTweets = {"bool":"false"};
//		localStorage["moreTweets"] = JSON.stringify(moreTweets);
		localStorage.moreTweets = "false";
		
		$.mobile.changePage('#course');		// load the page
	}	
}

